/*
 * =====================================================================================
 *
 *       Filename:  Memento.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2015年02月19日 13时14分57秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  LiuYi (), swliuyi@gmail.com
 *   Organization:  
 *
 * =====================================================================================
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <limits.h>
#include <iostream>
#include  <tr1/memory>

using namespace std;

class Memento
{
    private:
        string m_state;
    public:
        Memento(string state):m_state(state){}
        string GetState() const
        {
            return m_state;
        }
        void SetState(string state)
        {
            m_state = state;
        }
};

class Originator
{
    private:
        string m_state;
    public:
        string GetState() const
        {
            return m_state;
        }

        void SetState(string state)
        {
            m_state = state;
        }
        Memento * CreatMemento()
        {
            return (new Memento(m_state));
        }

        void SetMememto(const Memento & memento)
        {
            m_state = memento.GetState();
        }
        void show()
        {
            cout<<m_state<<endl;
        }
};

class Caretaker
{
    private:
        Memento m_memento;
    public:
        Caretaker():m_memento(""){};
        Memento GetMemento()
        {
            return m_memento;
        }
        void SetMememto(const Memento & memento)
        {
            m_memento = memento;
        }
};

int main(int argc, char *argv[])
{
    Originator o;
    o.SetState("On");
    o.show();

    Caretaker c;
    tr1::shared_ptr<Memento> mementoTemp(o.CreatMemento());
    c.SetMememto(*mementoTemp);

    o.SetState("off");
    o.show();
    
    o.SetMememto(*mementoTemp);
    o.show();
}
